package com.yy.controller;

import com.yy.mapper.UserMapper;
import com.yy.pojo.User;
import com.yy.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author YY
 */
@Controller
@RestController
public class UserController {
    @Autowired
    private UserService userService;


    @GetMapping
    public String loginPage(){
        return "Login";
    }

//    @PostMapping("/login")
//    public String login(@RequestParam String uname,
//                        @RequestParam String pwd,
//                        RedirectAttributes attributes) {
//
//        //获取当前的用户
//        Subject subject = SecurityUtils.getSubject();
//
//        //封装用户的登入数据
//        UsernamePasswordToken token = new UsernamePasswordToken(uname, DigestUtils.md5DigestAsHex(pwd.getBytes()));
//
//        //执行登入方法，如果没有异常则执行成功
//        try {
//            subject.login(token);
//            return "redirect:/file/showAll";
//        } catch (UnknownAccountException e) {
//            //表示用户名不存在
//            attributes.addFlashAttribute("message","用户名或者密码错误");
//            return "redirect:/user";
//        } catch (IncorrectCredentialsException e){
//            //表示密码错误
//            attributes.addFlashAttribute("message","用户名或者密码错误");
//            return "redirect:/user";
//        }
//    }
    @PostMapping("/login")
    public String login(User user, HttpSession session){
        User userDB = userService.login(user);
        if (userDB!=null){
            session.setAttribute("user", userDB);
            return "redirect:/file/showAll";
        }else {
            return "redirect:/login.html";
        }
    }


    @GetMapping("/getUserList")
    public List<User> getUserList(){
        List<User> userList = userService.getUserList();
        for (User user : userList){
            System.out.println(user);
        }
        return userList;
    }


    /**
     * 根据id选择用户
     */
    @GetMapping("/getUserByid")
    public String getUserByid(){
        User user = userService.getUserByid("001");
        System.out.println(user);
        return "ok";
    }

    /**
     * 添加一个用户
     */
//    @GetMapping("/addUser")
//    public String addUser(){
//        User user = new User("909", "hjio", "hujrhnn", "rmmr", "男", "编码", "组长");
//        userService.addUser(user);
//        return "ok";
//    }

    /**
     * 修改一个用户
     */
//    @GetMapping("/updateUser")
//    public String updateUser(){
//        User user = new User("001","ok","201610","","男","","");
//        userService.updateUser(user);
//        return "ok";
//    }

    /**
     * 根据id删除用户
     */
//    @GetMapping("/deleteUser")
//    public String deleteUser(){
//        userService.deleteUser("002");
//        return "mybatis-deleteUser:ok";
//    }

}
